package com.ychug.gateway.system;

import com.ychug.common.base.ResultAjaxHash;
import com.ychug.framework.web.base.BaseController;
import com.ychug.service.system.entity.SysMenuEntity;
import com.ychug.service.system.entity.SysRoleEntity;
import com.ychug.service.system.service.ISysMenuService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 菜单信息
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseController
{
    private String prefix = "modules/sys/menu";

    @Autowired
    private ISysMenuService menuService;

    @GetMapping()
    public String menu()
    {
        return prefix + "/viewMenu";
    }


    @GetMapping("/list")
    @ResponseBody
    public List<SysMenuEntity> list(SysMenuEntity menu)
    {
        List<SysMenuEntity> menuList = menuService.selectMenuList(menu);
        return menuList;
    }

    /**
     * 删除菜单
     */
    @PostMapping("/remove/{menuId}")
    @ResponseBody
    public ResultAjaxHash remove(@PathVariable("menuId") Long menuId)
    {
        if (menuService.selectCountMenuByParentId(menuId) > 0)
        {
            return error(1, "存在子菜单,不允许删除");
        }
        if (menuService.selectCountRoleMenuByMenuId(menuId) > 0)
        {
            return error(1, "菜单已分配,不允许删除");
        }
//        ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(menuService.deleteMenuById(menuId));
    }

    /**
     * 新增
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
    {
        SysMenuEntity menu = null;
        if (0L != parentId)
        {
            menu = menuService.selectMenuById(parentId);
        }
        else
        {
            menu = new SysMenuEntity();
            menu.setMenuId(0L);
            menu.setMenuName("主目录");
        }
        mmap.put("menu", menu);
        return prefix + "/addMenu";
    }

    /**
     * 新增保存菜单
     */

    @PostMapping("/add")
    @ResponseBody
    public ResultAjaxHash addSave(SysMenuEntity menu)
    {
        menu.setCreateBy("admin");

        return toAjax(menuService.insertMenu(menu));
    }

    /**
     * 修改菜单
     */
    @GetMapping("/edit/{menuId}")
    public String edit(@PathVariable("menuId") Long menuId, ModelMap mmap)
    {
        mmap.put("menu", menuService.selectMenuById(menuId));
        return prefix + "/editMenu";
    }

    /**
     * 修改保存菜单
     */

    @PostMapping("/edit")
    @ResponseBody
    public ResultAjaxHash editSave(SysMenuEntity menu)
    {
        menu.setUpdateBy("admin");

        return toAjax(menuService.updateMenu(menu));
    }

    /**
     * 选择菜单图标
     */
    @GetMapping("/icon")
    public String icon()
    {
        return prefix + "/icon";
    }

    /**
     * 校验菜单名称
     */
    @PostMapping("/checkMenuNameUnique")
    @ResponseBody
    public String checkMenuNameUnique(SysMenuEntity menu)
    {
        return menuService.checkMenuNameUnique(menu);
    }

    /**
     * 加载角色菜单列表树
     */
    @GetMapping("/roleMenuTreeData")
    @ResponseBody
    public List<Map<String, Object>> roleMenuTreeData(SysRoleEntity role)
    {
        List<Map<String, Object>> tree = menuService.roleMenuTreeData(role);
        return tree;
    }

    /**
     * 加载所有菜单列表树
     */
    @GetMapping("/menuTreeData")
    @ResponseBody
    public List<Map<String, Object>> menuTreeData(SysRoleEntity role)
    {
        List<Map<String, Object>> tree = menuService.menuTreeData();
        return tree;
    }

    /**
     * 选择菜单树
     */
    @GetMapping("/selectMenuTree/{menuId}")
    public String selectMenuTree(@PathVariable("menuId") Long menuId, ModelMap mmap)
    {
        mmap.put("menu", menuService.selectMenuById(menuId));
        return prefix + "/tree";
    }
}